package com.z.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.web.vo.request.PageVo;
import com.z.sys.entity.RolePermission;
import com.z.sys.mapper.RolePermissionMapper;
import com.z.sys.service.IRolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * 角色资源授权Service业务层处理
 *
 * @author z
 * @date 2023-03-02
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper,RolePermission> implements IRolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 查询角色资源授权
     * @param rolePermission 角色资源授权
     * @param pageVo
     * @return 角色资源授权 分页集合
     * */
    @Override
    public PageInfo<RolePermission> selectRolePermissionPage(RolePermission rolePermission, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<RolePermission> data = baseMapper.selectRolePermissionList(rolePermission);
        return new PageInfo<>(data);
    }

    @Override
    public List getRolePermission(int roleId) {
        return rolePermissionMapper.getRolePermission(roleId);
    }

    @Override
    public boolean saveRolePermisson(String roleId, List<String> permissionList) {
        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id", roleId));
        List<RolePermission> rolePermissions = new ArrayList<>();
        permissionList.forEach( o -> rolePermissions.add(new RolePermission(roleId, o)));
        return saveBatch(rolePermissions);
    }

}
